<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Api_good_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}
	
	public function count_shop_orders_count_by_good_id($good_id){
		$this->db->select('order_id');
		$this->db->from('shop_orders');
		$this->db->where('good_id',$good_id);
		$total=$this->db->count_all_results();
		return $total;
	}
	
	public function chu_shop_orders_count_by_good_id($good_id){
		$this->db->select_max('order_price');
		$this->db->where('good_id',$good_id);
		$this->db->where('order_status',1);
		$query = $this->db->get('shop_orders');
		return $query->row_array();
	}
	
	public function avg_shop_orders_count_by_good_id($good_id){
		$this->db->select_avg('order_price');
		$this->db->where('good_id',$good_id);
		$this->db->where('order_status',2);
		$query = $this->db->get('shop_orders');
		return $query->row_array();
	}

	public function get_goods_list_near($page=0,$limit=4,$web_category=0,$user_lat='',$user_lng='')
	{
		// $sql='select * from users_location where  
		// latitude > '.$lat.'-1 and  
		// latitude < '.$lat.'+1 and  
		// longitude > '.$lon.'-1 and  
		// longitude < '.$lon.'+1  
		// order by ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) *  COS((latitude * 3.1415) / 180 )
		// *COS(('.$lon.'* 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6380 asc limit 10';

		$start = $page*$limit;
		$this->db->select('a.*,b.shop_title');
		$this->db->from('shop_goods a');
		$this->db->join('shop_users b', 'b.shop_id = a.shop_id','left');
		if($web_category>0){
			$this->db->where('a.web_category',$web_category);
		}
		$this->db->where('a.good_lock',0);
		$this->db->where('a.good_stock >',0);
		if($user_lat && $user_lng){
			$this->db->where('a.good_lat >',$user_lat-1);
			$this->db->where('a.good_lat <',$user_lat+1);
			$this->db->where('a.good_lng <',$user_lng+1);
			$this->db->where('a.good_lng <',$user_lng+1);
			$this->db->order_by('ACOS(SIN(('.$user_lat.' * 3.1415) / 180 ) *SIN((good_lat * 3.1415) / 180 ) +COS(('.$user_lat.' * 3.1415) / 180 ) * COS((good_lat * 3.1415) / 180 ) *COS(('.$user_lng.' * 3.1415) / 180 - (good_lng * 3.1415) / 180 ) ) * 6380 asc');
		}else{
			$this->db->order_by('a.good_add_time','desc');
		}
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return $query->result_array();
		}
	}

	public function get_goods_list_nears($page=0,$limit=4,$web_category=0,$user_lat='',$user_lng='')
	{
		// select * from sb_user_geo order by ACOS(SIN(('.$city_lat.' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$city_lat.' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(('.$city_lng.' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380  asc  limit 30

		$start = $page*$limit;
		$this->db->select('a.*,b.shop_title');
		$this->db->from('shop_goods a');
		$this->db->join('shop_users b', 'b.shop_id = a.shop_id','left');
		if($web_category>0){
			$this->db->where('a.web_category',$web_category);
		}
		$this->db->where('a.good_lock',0);
		if($user_lat && $user_lng){
			$this->db->order_by('ACOS(SIN(('.$user_lat.' * 3.1415) / 180 ) *SIN((good_lat * 3.1415) / 180 ) +COS(('.$user_lat.' * 3.1415) / 180 ) * COS((good_lat * 3.1415) / 180 ) *COS(('.$user_lng.' * 3.1415) / 180 - (good_lng * 3.1415) / 180 ) ) * 6380 asc');	
		}else{
			$this->db->order_by('a.good_add_time','desc');
		}
		$this->db->where('a.good_stock >',0);
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return $query->result_array();
		}
	}

	//查询总数商品
    public function get_goods_list_count($web_category=0){
        $this->db->select('good_id');
        $this->db->from('shop_goods');
        if($web_category>0){
			$this->db->where('web_category',$web_category);
		}
		$this->db->where('good_stock >',0);
        $this->db->where('good_lock',0);
        $total=$this->db->count_all_results();
        return $total;
    }

	public function get_goods_list($page=0,$limit=4,$web_category=0,$ord='desc')
	{
		$start = $page*$limit;
		$this->db->select('a.*,b.shop_title');
		$this->db->from('shop_goods a');
		$this->db->join('shop_users b', 'b.shop_id = a.shop_id','left');
		if($web_category>0){
			$this->db->where('a.web_category',$web_category);
		}
		$this->db->where('a.good_lock',0);
		$this->db->where('a.good_stock >',0);
		$this->db->order_by('a.good_add_time',$ord);	
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return $query->result_array();
		}
	}

	public function get_gods_goods_list($page=0,$limit=4,$web_category=0,$ord='desc')
	{
		$start = $page*$limit;
		$this->db->select('a.*,b.shop_title');
		$this->db->from('shop_goods a');
		$this->db->join('shop_users b', 'a.shop_id = b.shop_id','left');
		if($web_category>0){
			$this->db->where('a.web_category',$web_category);
		}
		$this->db->where('a.good_lock',0);
		$this->db->where('a.good_stock >',0);
		$this->db->order_by('a.good_orders',$ord);	
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return $query->result_array();
		}
	}

	public function get_goods_list_gods($page=0,$limit=4,$web_category=0,$ord='desc')
	{
		$start = $page*$limit;
		$this->db->select('a.*,b.shop_title');
		$this->db->from('shop_goods a');
		$this->db->join('shop_users b', 'b.shop_id = a.shop_id','left');
		if($web_category>0){
			$this->db->where('a.web_category',$web_category);
		}
		$this->db->where('a.good_lock',0);
		$this->db->where('a.good_stock >',0);
		$this->db->order_by('a.good_orders',$ord);	
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return $query->result_array();
		}
	}

	public function get_popular_goods_list($page=0,$limit=6,$web_category=0,$ord='desc')
	{
		$start = $page*$limit;
		$this->db->select('a.*,b.shop_title');
		$this->db->from('shop_goods a');
		$this->db->join('shop_users b', 'b.shop_id = a.shop_id','left');
		if($web_category>0){
			$this->db->where('a.web_category',$web_category);
		}
		$this->db->where('a.good_lock',0);
		$this->db->where('a.good_stock >',0);
		$this->db->where('a.user_id >',0);
		$this->db->order_by('a.good_orders',$ord);	
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return $query->result_array();
		}
	}
	public function get_goods_free_count($web_category=0)
	{
		$this->db->select('good_id');
        $this->db->from('shop_goods');
        if($web_category>0){
			$this->db->where('web_category',$web_category);
		}
		$this->db->where('good_stock >',0);
        $this->db->where('good_lock',0);
		$this->db->where('good_voucher',1);
        $total=$this->db->count_all_results();
        return $total;
	}
	public function get_vouchers_goods_list($page=0,$limit=4,$good_voucher=1,$web_category=0,$ord='desc')
	{
		$start = $page*$limit;
		$this->db->select('a.*,b.shop_title');
		$this->db->from('shop_goods a');
		$this->db->join('shop_users b', 'b.shop_id = a.shop_id','left');
		if($web_category>0){
			$this->db->where('a.web_category',$web_category);
		}
		if($good_voucher>0){
			$this->db->where('a.good_voucher',1);
		}
		$this->db->where('a.good_lock',0);
		$this->db->where('a.good_stock >',0);
		$this->db->order_by('a.good_add_time',$ord);	
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return $query->result_array();
		}
	}
	public function get_god_goods_list($page=0,$limit=4,$web_category=0,$ord='desc')
	{
		$start = $page*$limit;
		$this->db->select('a.*,b.shop_title');
		$this->db->from('shop_goods a');
		$this->db->join('shop_users b', 'b.shop_id = a.shop_id','left');
		if($web_category>0){
			$this->db->where('a.web_category',$web_category);
		}
		$this->db->where('a.good_lock',0);
		$this->db->where('a.good_stock >',0);
		$this->db->order_by('a.good_god',$ord);	
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return $query->result_array();
		}
	}

}
